<?php

namespace App\Policies;

use App\Models\Repository;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class RepositoryPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function view(User $user, Repository $repository)
    {
        return $repository->type != $repository::TYPE_HIDDEN or $user->id == $repository->user_id;
    }

    public function create()
    {
        return true;
    }

    public function update(User $user, Repository $repository)
    {
        return $repository->user_id == $user->id;
    }

    public function delete(User $user, Repository $repository)
    {
        return $repository->user_id == $user->id;
    }

    public function createChapter(User $user, Repository $repository)
    {
        return $repository->user_id == $user->id or $repository->type
            == $repository::TYPE_FREE;
    }


    public function fork(User $user, Repository $repository)
    {
        return ($repository->type != $repository::TYPE_CLOSE and $repository->type != $repository::TYPE_HIDDEN);
    }

    public function push(User $user, Repository $repository)
    {
        return ($repository->is_fork and $repository->user_id == $user->id);
    }

    public function pull(User $user, Repository $repository)
    {
        return $repository->user_id == $user->id;
    }
}
